<?php

/**
 * Configuration of admin: constants, autoloading classes and error handling.
 *
 * @author Ian Clarence <ian.clarence@gmail.com>
 */

/**
 * Constants file.
 */
require_once dirname(__FILE__) . '/../../library/constants.php';

/**
 * Autoload function
 */
function __autoload($className) {

    /**
     * This directory: /admin/www/
     */
    $thisDir = dirname(__FILE__);

    /**
     * Path to Autoload class: /library/www/model/Autoload.php.
     */
    require_once dirname(__FILE__) . '/../../library/www/model/Autoload.php';

    /**
     * Search classes in /library/www/ and subdirectories.
     */
    $path = Autoload::findClass($thisDir . '/../../library/www', $className);

    /**
     * Search classes in /admin/www/ and subdirectories.
     */
    if ($path == NULL) {
            $path = Autoload::findClass($thisDir, $className);
    }

    /**
     * Load matching class.
     */
    require_once $path;
    
}

/**
 * Setting the error reporting level.
 */
error_reporting(E_ALL);

/**
 * Error handler function.
 */
function errorHandler(
    $errno,
    $errstr,
    $errfile,
    $errline,
    array $errorcontext
) {
    try {
        throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
    }
    catch (ErrorException $e) {
         die('Error no ' . $errno . ': ' . $errstr . ', in file ' . $errfile . ' on line ' . $errline);
    }
}

/**
 * Setting the error handling function.
 */
set_error_handler('errorHandler');

/**
 * Inintialising the system class, which contains database, session and statistics.
 */
$system = System::getInstance();
